//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite
{
    public static class MatrixExtensions
    {
        public static void Scale(ref Matrix matrix, ref Vector3 scale)
        {
            matrix.M11 *= scale.X;
            matrix.M12 *= scale.Y;
            matrix.M13 *= scale.Z;
            matrix.M21 *= scale.X;
            matrix.M22 *= scale.Y;
            matrix.M23 *= scale.Z;
            matrix.M31 *= scale.X;
            matrix.M32 *= scale.Y;
            matrix.M33 *= scale.Z;
            matrix.M41 *= scale.X;
            matrix.M42 *= scale.Y;
            matrix.M43 *= scale.Z;
        }

        public static void ScaleAt(ref Matrix matrix, ref Vector3 scale, ref Vector3 center)
        {
            float num = matrix.M14 * center.X;
            matrix.M11 = num + scale.X * (matrix.M11 - num);
            num = matrix.M14 * center.Y;
            matrix.M12 = num + scale.Y * (matrix.M12 - num);
            num = matrix.M14 * center.Z;
            matrix.M13 = num + scale.Z * (matrix.M13 - num);
            num = matrix.M24 * center.X;
            matrix.M21 = num + scale.X * (matrix.M21 - num);
            num = matrix.M24 * center.Y;
            matrix.M22 = num + scale.Y * (matrix.M22 - num);
            num = matrix.M24 * center.Z;
            matrix.M23 = num + scale.Z * (matrix.M23 - num);
            num = matrix.M34 * center.X;
            matrix.M31 = num + scale.X * (matrix.M31 - num);
            num = matrix.M34 * center.Y;
            matrix.M32 = num + scale.Y * (matrix.M32 - num);
            num = matrix.M34 * center.Z;
            matrix.M33 = num + scale.Z * (matrix.M33 - num);
            num = matrix.M44 * center.X;
            matrix.M41 = num + scale.X * (matrix.M41 - num);
            num = matrix.M44 * center.Y;
            matrix.M42 = num + scale.Y * (matrix.M42 - num);
            num = matrix.M44 * center.Z;
            matrix.M43 = num + scale.Z * (matrix.M43 - num);
        }

        public static void Translate(ref Matrix matrix, ref Vector3 offset)
        {
            matrix.M11 += matrix.M14 * offset.X;
            matrix.M12 += matrix.M14 * offset.Y;
            matrix.M13 += matrix.M14 * offset.Z;
            matrix.M21 += matrix.M24 * offset.X;
            matrix.M22 += matrix.M24 * offset.Y;
            matrix.M23 += matrix.M24 * offset.Z;
            matrix.M31 += matrix.M34 * offset.X;
            matrix.M32 += matrix.M34 * offset.Y;
            matrix.M33 += matrix.M34 * offset.Z;
            matrix.M41 += matrix.M44 * offset.X;
            matrix.M42 += matrix.M44 * offset.Y;
            matrix.M43 += matrix.M44 * offset.Z;
        }
    }
}
